/*
 * Copyright (c) 2013, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.index.unique;

import java.io.IOException;

import org.omorphdb.core.index.IndexType;
import org.omorphdb.core.io.Appender;
import org.omorphdb.core.io.Constants;
import org.omorphdb.core.io.Reader;
import org.omorphdb.core.io.Resource;
import org.omorphdb.core.io.Writer;
import org.omorphdb.core.value.ValueType;

/**
 * Implementation of {@link UniqueIndex} providing a perfect hash function,
 * using as key the unique document key mapped to the document position on the
 * storage file.
 */
public class DefaultIndex implements UniqueIndex<Long>
{
	private final Reader reader;
	private final Writer writer;
	private final Appender appender;

	public DefaultIndex(Resource resource) throws IOException
	{
		this.reader = resource.createReader();
		this.writer = resource.createWriter();
		this.appender = resource.createAppender();
	}

	@Override
	public IndexType getIndexType()
	{
		return IndexType.DEFAULT;
	}

	@Override
	public long search(Long key) throws IOException
	{
		this.reader.setCursor(key.longValue() * Constants.SIZEOF_LONG);

		return this.reader.readLong();
	}

	@Override
	public void register(Long key, long docPosition) throws IOException
	{
		this.writer.setCursor(key.longValue() * Constants.SIZEOF_LONG);
		this.writer.writeLong(docPosition);
	}

	@Override
	public void unregister(Long key) throws IOException
	{
		this.writer.setCursor(key.longValue() * Constants.SIZEOF_LONG);
		this.writer.writeLong(-1L);
	}

	/**
	 * Creates a default key archive whose archive is not yet used by other
	 * documents, then uses the created key to register the provided position.
	 * 
	 * @param position A document position within the storage file.
	 * 
	 * @return The created key
	 * @throws IOException
	 */
	public long registerFirstAvailableKey(long position) throws IOException
	{
		this.appender.reserve(Constants.SIZEOF_LONG);
		long idx = this.appender.getCursor() / Constants.SIZEOF_LONG;
		this.appender.appendLong(position);

		return idx;
	}

	@Override
	public ValueType getKeysType()
	{
		return ValueType.INT;
	}
}
